package com.fairytask.tools
{
	import com.fairytask.engine.managers.ResManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	
	public class ProcessTools extends Sprite
	{
		private var logoClass	:BitmapData;
		
		private var logoRes	:Bitmap;
		
		private var greyFilter	:ColorMatrixFilter;
		
		private var greyMask	:Shape;
		
		private var initX	:int	=	0;
		private var initY	:int	=	0;
		
		private var itemWidth	:int	=	0;
		private var itemHeight	:int	=	0;
		
		private var _process	:Number	=	0;
		
		private static var _instance	:ProcessTools;
		//是否初始化
		public var isInit:Boolean = false;
		
		public function ProcessTools()
		{
			super();
			initGUI();
		}
		
		/**
		 * 取得单例 
		 * @return 
		 * 
		 */
		public static function get instance():ProcessTools
		{
			if(null==_instance)
			{
				_instance	=	new ProcessTools();
			}
			return _instance;
		}
		
		private function initGUI():void
		{
			isInit	=	true;
			//加一层原色的
			var bgRes	:Bitmap	=	ResManager.newResClass("logoBitData") as Bitmap;
			
			logoRes	=	ResManager.newResClass("logoBitData") as Bitmap;
			itemWidth	=	logoRes.width;
			itemHeight	=	logoRes.height;
			bgRes.x	=	logoRes.x	=	initX;
			bgRes.y	=	logoRes.y	=	initY;
			addChild(bgRes);
			addChild(logoRes);
			greyFilter = new ColorMatrixFilter();    
			greyFilter.matrix = new Array(    
				1/3,1/3,1/3,0,0,    
				1/3,1/3,1/3,0,0,    
				1/3,1/3,1/3,0,0,    
				0,  0,  0,1,0);    
			logoRes.filters	=	[greyFilter];
			logoRes.cacheAsBitmap	=	true;
			
			//添加遮罩
			greyMask	=	new Shape();
			greyMask.graphics.beginBitmapFill(logoRes.bitmapData);
			greyMask.graphics.drawRect(0,0,itemWidth,itemHeight);
			greyMask.graphics.endFill();
			greyMask.cacheAsBitmap	=	true;
			greyMask.filters	=	[new BlurFilter(20, 20, BitmapFilterQuality.HIGH)];
			addChild(greyMask);
			greyMask.x	=	logoRes.x;
			greyMask.y	=	logoRes.y;
			logoRes.mask	=	greyMask;
		}
		
		/**
		 * 加载进度
		 * @param event
		 * 
		 */		
		protected function update(pProcess:Number):void
		{
			greyMask.x	=	initX+pProcess*itemWidth;
		}

		public function get process():Number
		{
			return _process;
		}

		public function set process(value:Number):void
		{
			value	=	value>1?1:value;
			value	=	value<0?0:value;
			_process = value;
			greyMask.x	=	initX+_process*itemWidth;
		}

	}
}